﻿using Cxuu.Common;
using Cxuu.Models;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Cxuu.Repository
{
    [UseDI(ServiceLifetime.Scoped)]
    public class AttmentRepository : BaseRepository<Attment, PetaPocoContext>
    {
        public AttmentRepository(PetaPocoContext petaPocoContext) : base(petaPocoContext) { }

        public async Task<Page<ViewAttment>> GetPageListAsync(int page, int limit, int uid)
        {
            Sql sql = Sql.Builder;
            sql.Select("A.Id,B.Username,B.Id as UserId ,A.Filename,A.Ext,A.Url,A.Size,A.Source,A.Uid,A.Time")
                .From("cxuu_attment as A")
                .LeftJoin("cxuu_admin_user as B").On("A.Uid = B.Id");
            if (uid != 0)
                sql.Where("A.Uid = @0", uid);

            sql.OrderBy("A.Id desc");
            return await CreateDAO().PageAsync<ViewAttment>(page, limit, sql);
        }

        public async Task<List<ViewAttment>> GetListAsync(string id)
        {
            Sql sql = Sql.Builder;
            sql.Select("*").From("cxuu_attment");
            sql.Where("Id IN (" + id + ")");
            sql.OrderBy("Id desc");
            return await CreateDAO().FetchAsync<ViewAttment>(sql);
        }

    }
}
